﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Aulac.Rsc.Bss;
using Aulac.Rsc.Bss.Tags;
using Aulac.Global.Common;

namespace Aulac.Client.Bss.WinForm.CapAss
{
    public partial class FormList_Dm_Staff : Aulac.Client.Controls.Forms.Bss.FormEmsListItemsData
    {
        private FormDlgStaff mFormDlgStaff = null;
        private string mTitleName = "";
        public FormList_Dm_Staff()
        {
            InitializeComponent();
            grdList.DataSource = new Aulac.Client.Data.Bss.DataSet.Catalog.Dm_StaffDataTable();
            string strTitleName = this.GetTileDmTypesName(Dm_TbName.Dm_Staff);
            if (!ValueHelper.isEmpty(strTitleName))
            {
                mTitleName = strTitleName;
                this.Text = Aulac.Global.Common.StringHelper.ReplaceIndex(strTitleName.ToLowerInvariant(), strTitleName.Substring(0, 1).ToLower(), strTitleName.Substring(0, 1).ToUpper(), 0);
            }
        }
       
        #region override Methods
        public override void OnFormLoad()
        {
            base.OnFormLoad();
            grdList.ClearColumsShow();
            grdList.AddColumnShow(new Aulac.UtilityTools.ColumnGridViewInfo() { ColCaption = "Mã nhân viên", ColName = tagItems.Staff.StaffID, DisplayIndex = 0 });
            grdList.AddColumnShow(new Aulac.UtilityTools.ColumnGridViewInfo() { ColCaption = "Tên nhân viên", ColName = tagItems.Staff.StaffName, DisplayIndex = 1 });
            grdList.AddColumnShow(new Aulac.UtilityTools.ColumnGridViewInfo() { ColCaption = "Giới tính", ColName = tagItems.SexStaff.SexName, DisplayIndex = 2 });
            grdList.AddColumnShow(new Aulac.UtilityTools.ColumnGridViewInfo() { ColCaption = "Ngày sinh", ColName = tagItems.Staff.StaffBirthday, DisplayIndex = 3 });
            grdList.AddColumnShow(new Aulac.UtilityTools.ColumnGridViewInfo() { ColCaption = "Quốc tịch", ColName = tagItems.Country.CountryID, DisplayIndex = 4 });
            grdList.AddColumnShow(new Aulac.UtilityTools.ColumnGridViewInfo() { ColCaption = "Nơi ở", ColName = tagItems.Staff.StaffPlace, DisplayIndex = 5 });
            grdList.AddColumnShow(new Aulac.UtilityTools.ColumnGridViewInfo() { ColCaption = "Địa chỉ", ColName = tagItems.Staff.StaffAddr, DisplayIndex = 6 });
            grdList.AddColumnShow(new Aulac.UtilityTools.ColumnGridViewInfo() { ColCaption = "Số điện thoại", ColName = tagItems.Staff.StaffPhone, DisplayIndex = 7 });
            grdList.AddColumnShow(new Aulac.UtilityTools.ColumnGridViewInfo() { ColCaption = "Email", ColName = tagItems.Staff.StaffEmail, DisplayIndex = 8 });
            grdList.AddColumnShow(new Aulac.UtilityTools.ColumnGridViewInfo() { ColCaption = "Chức năng", ColName = tagItems.Staff.StaffFunction, DisplayIndex = 9 });
            grdList.AddColumnShow(new Aulac.UtilityTools.ColumnGridViewInfo() { ColCaption = "Loại nhân viên", ColName = tagItems.StaffType.StaffTypeID, DisplayIndex = 10 });
            grdList.AddColumnShow(new Aulac.UtilityTools.ColumnGridViewInfo() { ColCaption = "Bộ phận", ColName = tagItems.Department.DeptID, DisplayIndex = 11 });
            grdList.AddColumnShow(new Aulac.UtilityTools.ColumnGridViewInfo() { ColCaption = "Ngày vào làm", ColName = tagItems.Staff.DateStartWork, DisplayIndex = 12 });
            grdList.AddColumnShow(new Aulac.UtilityTools.ColumnGridViewInfo() { ColCaption = "Ngày dừng làm", ColName = tagItems.Staff.DateStopWork, DisplayIndex = 13 });
            grdList.AddColumnShow(new Aulac.UtilityTools.ColumnGridViewInfo() { ColCaption = "Ngày vào Đảng", ColName = tagItems.Staff.DateInDang, DisplayIndex = 14 });
            grdList.AddColumnShow(new Aulac.UtilityTools.ColumnGridViewInfo() { ColCaption = "Chức vụ", ColName = tagItems.Staff.StaffPosition, DisplayIndex = 15 });
            grdList.AddColumnShow(new Aulac.UtilityTools.ColumnGridViewInfo() { ColCaption = "Còn làm việc", ColName = tagItems.Staff.IsActive, DisplayIndex = 16 });
            grdList.AddColumnShow(new Aulac.UtilityTools.ColumnGridViewInfo() { ColCaption = "Ghi chú", ColName = tagItems.Staff.Note, DisplayIndex = 17 });
            grdList.Columns[tagItems.Staff.StaffName].Width = 300;
            //grdList.Columns[tagItems.Staff.StaffName].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
        }
        protected override void OnSetDataSource(out object pDataSource, out string pTableName)
        {
            pDataSource = null;
            pTableName = TableNames.Dm_Staff;
        }
        protected override void OnAddParamsQuery()
        {
            //AddParamQuery(tagItems.Countries.IsActive, tagItems.Countries.IsActive, 1);// gia tri can truy van co isactive = 1;
        }

        protected override string OnCreateQueryString()
        {
            string sqlQuery = string.Format("SELECT St.StaffTypeName, C.CountryName, D.DeptName, St.StaffTypeID, ");
            sqlQuery += string.Format(" C.CountryID, D.DeptID, Staff.*");
            sqlQuery += string.Format(", Sx.{0}", tagItems.SexStaff.SexName);
            sqlQuery += string.Format(" FROM {0} AS Staff ", TableNames.Dm_Staff);
            sqlQuery += string.Format(" LEFT OUTER JOIN {0} AS C ON Staff.{1} = C.{2}", TableNames.Dm_Country, tagItems.Staff.CountryPrkID, tagItems.Country.CountryPrkID);
            sqlQuery += string.Format(" LEFT OUTER JOIN {0} D ON Staff.{1} = D.{2}", TableNames.Dm_Department, tagItems.Staff.DeptPrkID, tagItems.Department.DeptPrkID);
            sqlQuery += string.Format(" LEFT OUTER JOIN {0} AS St ON Staff.{1} = St.{2}", TableNames.Dm_StaffType, tagItems.Staff.StaffTypePrkID, tagItems.StaffType.StaffTypePrkID);
            sqlQuery += string.Format(" LEFT OUTER JOIN {0} AS Sx ON Staff.{1} = Sx.{2}", TableNames.Dm_SexStaff, tagItems.Staff.SexID, tagItems.SexStaff.SexID);
            return sqlQuery;
        }

        protected override void OnListShowDetailVoucher()
        {
            ProcessFormCmd(Aulac.Global.Enums.FormModes.ViewMode);
        }
        protected override void OnListAddNewVoucher()
        {
            ProcessFormCmd(Aulac.Global.Enums.FormModes.NewMode);
            mFormDlgStaff.ShowDialog();
        }
        protected override void OnListEditVoucher()
        {
            ProcessFormCmd(Aulac.Global.Enums.FormModes.EditMode);
        }
        protected override void OnListDelDataVoucher()
        {
            ProcessFormCmd(Aulac.Global.Enums.FormModes.DelMode);
        }
        #endregion
        #region Methods
        private void ProcessFormCmd(Aulac.Global.Enums.FormModes pFormmode)
        {
            try
            {
                if (mFormDlgStaff == null)
                {
                    mFormDlgStaff = new FormDlgStaff();
                    FormDataItemsCmd = mFormDlgStaff;
                    mFormDlgStaff.ExitComplated += new EventHandler(FrmDlgStaff_ExitComplete);                                
                }                
                if (!ValueHelper.isEmpty(mTitleName))
                    mFormDlgStaff.TitleName = mTitleName;
                mFormDlgStaff.StaffPrkID = grdList.GetValue(grdList.SelectedGridRowView, tagItems.Staff.StaffPrkID);
                mFormDlgStaff.FormMode = pFormmode;

                if (pFormmode == Aulac.Global.Enums.FormModes.ViewMode)
                {
                    mFormDlgStaff.ShowDetailVoucher();
                }
                else if (pFormmode == Aulac.Global.Enums.FormModes.NewMode)
                {
                    mFormDlgStaff.StaffPrkID = "";
                    mFormDlgStaff.AddNewVoucher();
                }
                else if (pFormmode == Aulac.Global.Enums.FormModes.EditMode)
                {
                    mFormDlgStaff.EditVoucher();
                }
                else if (pFormmode == Aulac.Global.Enums.FormModes.DelMode)
                {
                    mFormDlgStaff.DeleteStaff();
                    this.RefreshList();
                    return;
                }
                mFormDlgStaff.ShowDialog();
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }
        private void FrmDlgStaff_ExitComplete(object sender, EventArgs e)
        {
            this.CancelTransaction();
            this.grdList.ClearDataContainer();
            this.RefreshList();
        }
        #endregion
    }
}
